{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ef1fba3e-934f-4c48-9fd8-9450cf46dfa4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5943664-bb22-468f-acbd-f8b7da0d5a86",
   "metadata": {},
   "source": [
    "一维数组转换成二维数组-reshape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4f375cd4-34b6-4a6d-b9c9-4223c784eba2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(12)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fefd3b77-57ec-4343-9d9b-b28e27054da2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(3,4) #设置数组形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "37091d04-ea6e-4513-bed6-0cfb3fb75254",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(12).reshape(3,4) #与上面等价\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6cadd7fd-1079-42fb-8d5d-7adc4fbc829e",
   "metadata": {},
   "source": [
    "将二维数组展平为一维数组-----ravel横向"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bc07d819-3f09-4c34-b9e5-c3f4ab779f23",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.ravel() # 横向"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86e01f33-e64e-43b9-ae2d-3e914d566b84",
   "metadata": {},
   "source": [
    "将二维数组展平为一维数组-----rflatten（横向&纵向）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "378d5256-db5a-4dfe-969a-5b87230eea19",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  4,  8,  1,  5,  9,  2,  6, 10,  3,  7, 11])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.flatten('F') # F表示纵向"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f688c1b2-05dd-4ae9-b3a3-0ea5a08287bb",
   "metadata": {},
   "source": [
    "数组横向&纵向的组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f98725c5-f866-4451-a000-41706b316513",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(10).reshape(2,5)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "4171267a-5945-4e45-b852-da9fa5ccb6be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0. , 0.1, 0.2, 0.3, 0.4],\n",
       "       [0.5, 0.6, 0.7, 0.8, 0.9]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.linspace(0,1,endpoint=False,num=10).reshape(2,5)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0e7c8981-e660-4699-bf42-e021b8bda0c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0. , 1. , 2. , 3. , 4. ],\n",
       "       [5. , 6. , 7. , 8. , 9. ],\n",
       "       [0. , 0.1, 0.2, 0.3, 0.4],\n",
       "       [0.5, 0.6, 0.7, 0.8, 0.9]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((a,b)) # 上下堆叠"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "94a80516-2dd4-4673-a6ba-70b7ecfef522",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0. , 1. , 2. , 3. , 4. , 0. , 0.1, 0.2, 0.3, 0.4],\n",
       "       [5. , 6. , 7. , 8. , 9. , 0.5, 0.6, 0.7, 0.8, 0.9]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "4c34e949-862a-477e-b8f4-55e93ee6f56e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0. , 1. , 2. , 3. , 4. ],\n",
       "       [5. , 6. , 7. , 8. , 9. ],\n",
       "       [0. , 0.1, 0.2, 0.3, 0.4],\n",
       "       [0.5, 0.6, 0.7, 0.8, 0.9]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate((a,b),axis=0) # 默认纵向"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "84c56989-dfd8-4b62-ac6e-767854dbf52f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0. , 1. , 2. , 3. , 4. , 0. , 0.1, 0.2, 0.3, 0.4],\n",
       "       [5. , 6. , 7. , 8. , 9. , 0.5, 0.6, 0.7, 0.8, 0.9]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate((a,b),axis=1) # 横向"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16145f24-b9f1-4999-9eaf-b432d3494b03",
   "metadata": {},
   "source": [
    "切割数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "b4f12b5a-7664-4335-8ff6-f9a62ceaefd0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(16).reshape(4,4)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "7693ff7a-a7fd-4e6b-95ce-cad56fef6853",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 0,  1],\n",
       "        [ 4,  5],\n",
       "        [ 8,  9],\n",
       "        [12, 13]]),\n",
       " array([[ 2,  3],\n",
       "        [ 6,  7],\n",
       "        [10, 11],\n",
       "        [14, 15]])]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(a,2) # 横向切割将数组a 水平分割成2个相等的部分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "23ce9298-5fb8-4a1e-8a9f-27928756c326",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[0, 1, 2, 3],\n",
       "        [4, 5, 6, 7]]),\n",
       " array([[ 8,  9, 10, 11],\n",
       "        [12, 13, 14, 15]])]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(a,2) # 纵向分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "f166b286-56f5-421c-934d-629488ae52a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[0, 1, 2, 3],\n",
       "        [4, 5, 6, 7]]),\n",
       " array([[ 8,  9, 10, 11],\n",
       "        [12, 13, 14, 15]])]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(a,2,axis=0) # 纵向分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "55c19e34-2870-4f7f-99c1-60a06c6c4c40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 0,  1],\n",
       "        [ 4,  5],\n",
       "        [ 8,  9],\n",
       "        [12, 13]]),\n",
       " array([[ 2,  3],\n",
       "        [ 6,  7],\n",
       "        [10, 11],\n",
       "        [14, 15]])]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(a,2,axis=1) # 横向分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de0ef4a1-0ce1-4955-b809-94d684fbfe95",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
